package NewTmas.EditMode;

import NewTmas.Graphs.GraphVizGraph;
import att.grappa.Node;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.Arrays;

/**
 * Created by IntelliJ IDEA.
 * User: abhatnag
 * Date: 8/18/11
 * Time: 6:26 PM
 * To change this template use File | Settings | File Templates.
 */
public class SplitParallelListener implements ActionListener
{

    String jobname;
    GraphVizGraph graphVizGraph;
    Connection connection;
    EditIntFrame editIntFrame;

    public SplitParallelListener(String jobname, GraphVizGraph graphVizGraph, Connection connection, EditIntFrame editIntFrame)
    {
        this.jobname = jobname;
        this.graphVizGraph = graphVizGraph;
        this.connection = connection;
        this.editIntFrame = editIntFrame;
    }

    public void actionPerformed(ActionEvent e)
    {
        Node[] nodes = graphVizGraph.getGraph().nodeElementsAsArray();
        String[] currentNodeNames = new String[nodes.length];
        for (int i = 0; i < nodes.length; i++)
        {
            currentNodeNames[i] = nodes[i].getName();

        }
        Arrays.sort(currentNodeNames);
        JComboBox cbox1 = new JComboBox(currentNodeNames);
        JTextField newTaskfield = new JTextField();

        Object[] message = new Object[]{"Task Tag", cbox1, "Parallel Task", newTaskfield};
        int input = JOptionPane.showConfirmDialog(null, message, "Select the task to duplicate as parallel", JOptionPane.OK_CANCEL_OPTION);
        if (input == JOptionPane.OK_OPTION)
        {
            String taskName = cbox1.getSelectedItem().toString();
            String newtask = newTaskfield.getText();

            CallStoredProc storedProc = new CallStoredProc("{call tms.task_util.split_parallel(?,?,?)}", new String[]{jobname, taskName, newtask}, connection);

            editIntFrame.sqlCommands.add(storedProc);
            editIntFrame.model.addElement(storedProc);
            editIntFrame.sqlcommandsList.setModel(editIntFrame.model);

        }
    }
}
